﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessEntity;

namespace BusinessService
{
    /// <summary>
    /// 维修受理表
    /// </summary>
    public class RepairsService
    {
        #region 新增
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddNew(Repairs model)
        {
            object obj = SqlHelper.ExecuteNonQuery(
                "INSERT INTO Repairs(WordOrderId,CompId,ArticleId,statusid,ApplyEmp,ApplyTime,Phone,Email,ApplyAddres,troubledesc) VALUES (@WordOrderId,@CompId,@ArticleId,'1',@ApplyEmpName,@ApplyTime,@Phone,@Email,@ApplyAddres,@troubledesc)"
                , new SqlParameter("@WordOrderId", model.WordOrderId)
                , new SqlParameter("@CompId", model.CompId)
                , new SqlParameter("@ArticleId", model.Article.ArticleId)
                , new SqlParameter("@ApplyEmpName", model.ApplyEmpName)
                , new SqlParameter("@ApplyTime", model.ApplyTime)
                , new SqlParameter("@Phone", model.Phone)
                , new SqlParameter("@Email", model.Email)
                , new SqlParameter("@ApplyAddres", model.ApplyAddres)
                ,new SqlParameter("@troubledesc",model.TroubleDesc)
            );
            return Convert.ToInt32(obj);
        }
        #endregion

        public int GetIdByWorkId(string workid)
        {
            var sql = "select id from Repairs where WordOrderId='" + workid+"'";
            return (int) SqlHelper.ExecuteScalar(sql);
        }

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            int rows = SqlHelper.ExecuteNonQuery("DELETE FROM Repairs WHERE Id = @id", new SqlParameter("@id", id));
            return rows > 0;
        }
        #endregion
        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(Repairs model)
        {
            string sql = "UPDATE Repairs SET WordOrderId=@WordOrderId,CompId=@CompId,Article=@Article,RepEmpName=@RepEmpName,FinishTime=@FinishTime,StatusId=@StatusId,ApplyEmpName=@ApplyEmpName,ApplyTime=@ApplyTime,Phone=@Phone,Email=@Email,ApplyAddres=@ApplyAddres,WhoEmpName=@WhoEmpName,Evaluate=@Evaluate WHERE Id=@Id";
            int rows = SqlHelper.ExecuteNonQuery(sql
                , new SqlParameter("@Id", model.Id)
                , new SqlParameter("@WordOrderId", model.WordOrderId)
                , new SqlParameter("@CompId", model.CompId)
                , new SqlParameter("@Article", model.Article)
                , new SqlParameter("@RepEmpName", model.RepEmpName)
                , new SqlParameter("@FinishTime", model.FinishTime)
                , new SqlParameter("@StatusId", model.StatusId)
                , new SqlParameter("@ApplyEmpName", model.ApplyEmpName)
                , new SqlParameter("@ApplyTime", model.ApplyTime)
                , new SqlParameter("@Phone", model.Phone)
                , new SqlParameter("@Email", model.Email)
                , new SqlParameter("@ApplyAddres", model.ApplyAddres)
                , new SqlParameter("@WhoEmpName", model.WhoEmpName)
                , new SqlParameter("@Evaluate", model.Evaluate)
            );
            return rows > 0;
        }
        #endregion
        #region 根据Id查询
        /// <summary>
        /// 根据Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Repairs Get(int id)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM Repairs WHERE Id=@Id", new SqlParameter("@Id", id));
            if (dt.Rows.Count > 1)
            {
                throw new Exception("more than 1 row was found");
            }
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            DataRow row = dt.Rows[0];
            Repairs model = ToModel(row);
            return model;
        }
        #endregion

        public Repairs GetByOrder(string order)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM Repairs WHERE WordOrderId=@Id", new SqlParameter("@Id", order));
            if (dt.Rows.Count > 1)
            {
                throw new Exception("more than 1 row was found");
            }
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            DataRow row = dt.Rows[0];
            Repairs model = ToModel(row);
            return model;
        }

        #region 查询
        /// <summary>
        /// 查询所有
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private static Repairs ToModel(DataRow row)
        {
            Repairs model = new Repairs();
            model.Id = (int)row["Id"];
            model.WordOrderId = (string)row["WordOrderId"];
            model.CompId = (string)row["CompId"];
            model.Article = new Article() { ArticleId =(int) row["ArticleId"] };
            model.RepEmpName = row["RepEmp"] == DBNull.Value ? 0 : (int)row["RepEmp"];
            if (row["FinishTime"] != DBNull.Value)
            {
                model.FinishTime = (DateTime)(row["FinishTime"]);
            }
            model.ApplyEmpName = (int)row["ApplyEmp"];
            model.ApplyTime = (DateTime)row["ApplyTime"];
            model.Phone = (string)row["Phone"];
            model.Email = (string)row["Email"];
            model.ApplyAddres = (string)row["ApplyAddres"];
            if (row["FinishTime"] != DBNull.Value)
            {
                model.WhoEmpName = (int)row["WhoEmpName"];
            }
            if (row["Evaluate"] != DBNull.Value)
            {
                model.Evaluate = (string)row["Evaluate"];
            }
            return model;
        }

        public IEnumerable<Repairs> ListAll()
        {
            List<Repairs> list = new List<Repairs>();
            DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM Repairs");
            foreach (DataRow row in dt.Rows)
            {
                list.Add(ToModel(row));
            }
            return list;
        }
        #endregion

        public int AsignOrder(string workorder, int userid)
        {
            var sql = "update repairs set repemp=@repemp,statusid=2,asigntime=@asigntime where WordOrderId=@workorder";
            return SqlHelper.ExecuteNonQuery(sql, 
                new SqlParameter("@repemp",userid),
                new SqlParameter("@asigntime",DateTime.Now),
                new SqlParameter("@workorder",workorder)
                );
        }

        public int UpdateRepByWorkId(string workid, int repid)
        {
            return
                SqlHelper.ExecuteNonQuery("update repairs set repemp=" + repid + " where wordOrderId='" + workid + "'");
        }

        public int UpdateRepairStatus(string workid, int statusid)
        {
            return
                SqlHelper.ExecuteNonQuery("update repairs set statusid=" + statusid + " where wordOrderId='" + workid + "'");
       
        }

        public object ExecuteSql(string sql)
        {
            return SqlHelper.ExecuteDataTable(sql);
        }
    }
}
